{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2EU6oCTeg-VK"
      },
      "outputs": [],
      "source": [
        "# If needed, install openai\n",
        "#!pip install openai"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "lshGm9Ooht31"
      },
      "outputs": [],
      "source": [
        "# Import libraries\n",
        "import openai\n",
        "import pandas as pd"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "e5SbcWd30Sio",
        "outputId": "1f2d6b48-73c5-4e48-ce51-d199e0423b9a"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n"
          ]
        }
      ],
      "source": [
        "# Set up folder where there reviews are\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive', force_remount=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RpDarVkB0YbO",
        "outputId": "f075d4f0-79ee-4d79-e3db-7a13d94a61c1"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/drive/MyDrive/weimar_films_datasets\n"
          ]
        }
      ],
      "source": [
        "%cd /content/drive/MyDrive/weimar_films_datasets/"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Enter API key\n",
        "api_key = input(\"Please enter your API key: \")"
      ],
      "metadata": {
        "id": "RpCMs0lAfTR7"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ysds8-rguQde"
      },
      "outputs": [],
      "source": [
        "# Set up your key\n",
        "openai.api_key = api_key"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Load data\n",
        "df = pd.read_excel('Reviews Corpus.xlsx', index_col=[0])"
      ],
      "metadata": {
        "id": "T1-BtYDuw3Ab"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "The prompt was: \"The following is a film review. Is it negative or positive? Explain your reasons, and keep in mind that negative emotions may be warranted depending on the genre of the film, and do not necessarily signify a negative review. Review:\""
      ],
      "metadata": {
        "id": "G_9j5fG75c-_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Set up function to analyze sentiment\n",
        "def analyze_sentiment(review_text):\n",
        "    prompt = \"The following is a film review. Is it negative or positive? Explain your reasons, and keep in mind that negative emotions may be warranted depending on the genre of the film, and do not necessarily signify a negative review. Review:\"\n",
        "    input_text = prompt + \"\\n\" + review_text\n",
        "\n",
        "    try:\n",
        "        response = openai.ChatCompletion.create(\n",
        "            model=\"gpt-3.5-turbo\",\n",
        "            messages=[\n",
        "                {\"role\": \"user\", \"content\": input_text},\n",
        "            ]\n",
        "        )\n",
        "        sentiment = response['choices'][0]['message']['content']\n",
        "        return sentiment\n",
        "    except Exception as e:\n",
        "        print(f\"Error: {e}\")\n",
        "        return \"Error\""
      ],
      "metadata": {
        "id": "kJ6l52Q3wGJF"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Apply function to the dataframe\n",
        "df[\"ChatGPT API Answers\"] = df[\"Review_Eng\"].apply(lambda review: analyze_sentiment(review))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2wtgq9K6wKm9",
        "outputId": "fd06dc12-4acf-4582-d67c-6529c0ebcecc"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Error: This model's maximum context length is 4097 tokens. However, your messages resulted in 4381 tokens. Please reduce the length of the messages.\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "This error happened to review 9W4Z3TFU, from “Film Notes”. We ran that one directly on ChatGPT then (since there are some extra tokens used through the API) and manually copied the results into the table, using the same prompt.\n"
      ],
      "metadata": {
        "id": "C-XTdeX_Vdra"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df.to_excel(\"Reviews Corpus.xlsx\")"
      ],
      "metadata": {
        "id": "_DFYDTiawRwg"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}